/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Author:
 *     coolair.loveU2@gmail.com
 *******************************************************************************/

package org.skyfree.ghyll.tcard.core.internal;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.skyfree.ghyll.tcard.core.IDrawing;
import org.skyfree.ghyll.tcard.core.IMaterial;
import org.skyfree.ghyll.tcard.core.IProcess;
import org.skyfree.ghyll.tcard.core.IStoragePath;
import org.skyfree.ghyll.tcard.core.ITCard;
import org.skyfree.ghyll.tcard.core.ITWorkpiece;
import org.skyfree.ghyll.tcard.core.TCardCore;

/**
 * This class ...
 */

public class TCard extends AbstractTResource implements ITCard {

	ITCard parent;
	boolean composite;
	ArrayList<ITCard> subCard;
	ITWorkpiece workPiece;

	String itemID;
	String name;
	IMaterial material;
	IDrawing sketch;
	IDrawing bluePrint;
	String phase = ITCard.PhaseDesign;
	String amount = "1";
	String owner = "";
	String remark;
	boolean finished = false;

	private ArrayList<IProcess> workingProcess;
	private boolean initialize;

	public TCard(IStoragePath path, ITCard parent, String name,
			ITWorkpiece workPiece) {
		super(path);
		this.name = name;
		this.parent = parent;
		if (parent != null)
			parent.getSubTCard().add(this);
		this.subCard = new ArrayList<ITCard>();
		this.workingProcess = new ArrayList<IProcess>();
		this.workPiece = workPiece;
	}

	@Override
	public IDrawing getBlurprint() {
		return this.bluePrint;
	}

	@Override
	public void setBlurprint(IDrawing bluePrint) {
		this.bluePrint = bluePrint;
	}

	@Override
	public String getItemID() {
		return this.itemID;
	}

	@Override
	public void setItemID(String itemID) {
		this.itemID = itemID;
	}

	@Override
	public String getItemName() {
		return this.name;
	}

	@Override
	public void setItemName(String name) {
		this.name = name;
	}

	@Override
	public IMaterial getMaterial() {
		return this.material;
	}

	@Override
	public void setMaterial(IMaterial material) {
		this.material = material;
	}

	@Override
	public ITCard getParent() {
		return this.parent;
	}

	@Override
	public IDrawing getSketch() {
		return this.sketch;
	}

	@Override
	public void setSketch(IDrawing sketch) {
		this.sketch = sketch;
	}

	@Override
	public int getStatus() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ArrayList<ITCard> getSubTCard() {
		return this.subCard;
	}

	@Override
	public ArrayList<IProcess> getWorkingProcess() {
		return this.workingProcess;
	}

	@Override
	public void setWorkingProcess(ArrayList<IProcess> workingProcess) {
		this.workingProcess = workingProcess;
	}

	@Override
	public ITWorkpiece getWorkpiece() {
		return this.workPiece;
	}

	@Override
	public boolean isCompositeTCard() {
		return this.composite;
	}

	@Override
	public void setComposite(boolean composite) {
		this.composite = composite;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void load() throws CoreException {
		try {
			this.provider.load(this);
		} catch (Exception e) {
			this.log(e);
			throw new CoreException(new Status(IStatus.ERROR,
					TCardCore.PLUGIN_ID, e.getMessage()));
		}

	}

	@Override
	public void reload() throws CoreException {
		this.load();
	}

	@Override
	public void save() throws CoreException {
		try {
			this.provider.save(this);
		} catch (Exception e) {
			this.log(e);
			throw new CoreException(new Status(IStatus.ERROR,
					TCardCore.PLUGIN_ID, e.getMessage()));
		}
	}

	@Override
	public String getPhase() {
		return this.phase;
	}

	@Override
	public void delete() {
		if (this.isCompositeTCard()) {
			ArrayList<ITCard> allSub = new ArrayList<ITCard>();
			allSub.addAll(this.getSubTCard());
			Iterator<ITCard> it = allSub.iterator();
			while (it.hasNext()) {
				it.next().delete();
			}
		}
		try {

			if (this.parent != null) {
				this.parent.getSubTCard().remove(this);
				//((TCard)this.parent).deleteSubCard(this);
			} else {
				((TWorkpiece) this.workPiece).rootCard = null;
			}
			((TWorkpiece) this.workPiece).deleteMap(this);
			this.provider.delete(this);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	void deleteSubCard(ITCard sub) {

	}

	@Override
	public void moveAround(boolean up, ITCard card) {
		int index = this.subCard.indexOf(card);
		this.subCard.remove(card);
		if (up)
			this.subCard.add(index - 1, card);
		else
			this.subCard.add(index + 1, card);

	}

	@Override
	public String getAmount() {
		return this.amount;
	}

	@Override
	public String getOwner() {
		return this.owner;
	}

	@Override
	public void setAmount(String amount) {
		this.amount = amount;

	}

	@Override
	public void setOwner(String owner) {
		this.owner = owner;
	}

	@Override
	public String getRemark() {
		return remark;
	}

	@Override
	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Override
	public void setParent(ITCard parent) {
		if (this.parent != null)
			this.parent.getSubTCard().remove(this);
		this.parent = parent;
		this.parent.getSubTCard().add(this);

	}

	@Override
	public boolean isFinished() {
		if (!initialize) {
			this.initialize = true;
			this.updateFinish();
		}
		return this.finished;
	}

	@Override
	public ITCard updateFinish() {
		if (this.checkFinish()) {
			this.finished = true;
			if (this.parent != null) {
				ITCard isUpdated = this.parent.updateFinish();
				if (isUpdated != null)
					return isUpdated;
				else
					return this;
			} else
				return this;
		} else
			return null;
	}

	private boolean checkFinish() {
		Iterator<ITCard> it = this.subCard.iterator();
		while (it.hasNext()) {
			if (!it.next().isFinished())
				return false;
		}
		Iterator<IProcess> itPro = this.getWorkingProcess().iterator();
		while (itPro.hasNext()) {
			if (!itPro.next().isFinished())
				return false;
		}
		return true;
	}

	@Override
	public void setFinished(boolean finished) {
		this.finished = false;

	}
}
